<script lang="ts" setup>
import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { SystemMenuApi } from '#/api';

import { Page, useVbenDrawer } from '@vben/common-ui';
import { Plus } from '@vben/icons';

import { Button, message } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteMenu, getAllMenuList } from '#/api/system';
import { $t } from '#/locales';

import { useColumns, useGridFormSchema } from './data';
import Form from './modules/form.vue';

const [FormDrawer, formDrawerApi] = useVbenDrawer({
  connectedComponent: Form,
  destroyOnClose: true,
});

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    schema: useGridFormSchema(),
    submitOnChange: true,
  },
  gridOptions: {
    columns: useColumns(onActionClick),
    height: 'auto',
    keepSource: true,
    resizable: true,
    showOverflow: 'tooltip',
    pagerConfig: {
      enabled: false,
    },
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          const queryParams = {
            page: page.currentPage,
            page_size: page.pageSize,
            ...formValues,
          };

          return await getAllMenuList(queryParams);
        },
      },
    },
    rowConfig: {
      keyField: 'id',
    },
    toolbarConfig: {
      custom: true,
      export: false,
      refresh: true,
      search: true,
      zoom: true,
    },
    treeConfig: {
      parentField: 'parent_id',
      rowField: 'id',
      transform: false,
    },
  } as VxeTableGridOptions<SystemMenuApi.Menu>,
});

function onActionClick(e: OnActionClickParams<SystemMenuApi.Menu>) {
  switch (e.code) {
    case 'append': {
      onAppend(e.row);
      break;
    }
    case 'delete': {
      onDelete(e.row);
      break;
    }
    case 'edit': {
      onEdit(e.row);
      break;
    }
  }
}

function onEdit(row: SystemMenuApi.Menu) {
  formDrawerApi.setData(row).open();
}

function onAppend(row: SystemMenuApi.Menu) {
  formDrawerApi.setData({ parent_id: row.id }).open();
}

async function onDelete(row: SystemMenuApi.Menu) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', [row.name]),
    duration: 0,
    key: 'action_process_msg',
  });

  await deleteMenu(row.id!)
    .then(() => {
      message.success({
        content: $t('ui.actionMessage.deleteSuccess', [row.name]),
        key: 'action_process_msg',
      });
      onRefresh();
    })
    .catch(() => {
      hideLoading();
    });
}

function onRefresh() {
  gridApi.query();
}

function onCreate() {
  formDrawerApi.setData({}).open();
}
</script>

<template>
  <Page auto-content-height>
    <FormDrawer @success="onRefresh" />

    <Grid :table-title="$t('system.menu.list')" class="menu-grid">
      <template #toolbar-tools>
        <Button type="primary" @click="onCreate">
          <Plus class="size-5" />
          {{ $t('ui.actionTitle.create', [$t('system.menu.addLabel')]) }}
        </Button>
      </template>
    </Grid>
  </Page>
</template>

<style scoped>
/* 响应式优化 */
@media (max-width: 768px) {
  .menu-grid :deep(.vxe-table--body-wrapper) {
    overflow-x: auto;
  }

  .menu-grid :deep(.vxe-table) {
    min-width: 800px;
  }
}

.menu-grid {
  width: 100%;
  overflow-x: auto;
}

/* 确保表格内容不会溢出 */
.menu-grid :deep(.vxe-cell) {
  word-break: break-word;
  white-space: normal;
}

/* 树节点缩进优化 */
.menu-grid :deep(.vxe-tree--indent) {
  width: 16px;
}
</style>
